package com.ykh.controller;

import com.ykh.pojo.dto.RegisterParam;
import com.ykh.pojo.po.User;
import com.ykh.pojo.vo.ErrorCode;
import com.ykh.pojo.vo.Result;
import com.ykh.service.UserService;
import com.ykh.util.UserThreadLocal;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ykh
 * @since 2023年10月26日
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    /**
     * 用户登录接口
     * @return
     */
    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    public Result Login(
            @RequestParam @ApiParam("用户账号") String account,
            @RequestParam @ApiParam("用户密码")  String password) {
        return  userService.login(account,password);
    }

    /**
     * 用户注册接口
     * @return
     */
    @ApiOperation("用户注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterParam registerParam){
        return userService.register(registerParam);
    }

    /**
     * 用户获取自己信息接口
     * @return
     */
    @ApiOperation("用户获取自己信息接口接口")
    @GetMapping("/info")
    public Result getInfo(){
        User user = UserThreadLocal.get();
        if(user == null){
            return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
        }
        return Result.success(userService.getInfo(user.getAccount()));
    }


    /**
     * 用户退出登录接口
     * @return
     */
    @ApiOperation("用户退出登录接口")
    @GetMapping("/logout")
    public Result logout(@RequestHeader("Authorization") String token){
        User user = UserThreadLocal.get();
        if(user == null){
            return Result.fail(ErrorCode.NO_LOGIN.getCode(), ErrorCode.NO_LOGIN.getMsg());
        }
        return Result.success(userService.logout(token));
    }



}

